package com.hiwan.unions.view.popupWindow;

import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.main.MainActivity;
import com.hiwan.unions.adapter.fragmentAdapters.FragmentAdapter_POP;
import com.hiwan.unions.util.PerfectClickListener;
import com.hiwan.unions.util.StartIntent;
import com.hiwan.unions.util.Utils;

import java.lang.reflect.Method;

/**
 * Created by llg on 2017/7/8.
 */
public class ButtonPop extends PopupWindow {
    private MainActivity context;
    private View window;
    private GridView gridView;
    private RadioGroup group;
    private FragmentAdapter_POP adapter;
    private int[] imageUrls = {R.mipmap.jyfw_2, R.mipmap.cyfw, R.mipmap.knxf_2, R.mipmap.hzbz,
            R.mipmap.flbz, R.mipmap.flyz, R.mipmap.flzsk, R.mipmap.mmbbw_2,
            R.mipmap.hljy, R.mipmap.xlyz_2, R.mipmap.zccx_2, R.mipmap.ndbh_2,
            R.mipmap.jnpx_2, R.mipmap.fwzd_2, R.mipmap.spkt_2, R.mipmap.dzsw,
            R.mipmap.lyy_2};
    public String[] title;
    private RadioButton button;

    public ButtonPop(final MainActivity context) {
        this.context = context;
        window = LayoutInflater.from(context).inflate(R.layout.window_button, null);
        this.setContentView(window);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setAnimationStyle(R.style.mypopwindow_anim_style);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.update();
        window.setFocusable(true);
        window.setFocusableInTouchMode(true);
        window.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK)
                    dismiss();
                return false;
            }
        });
        this.setBackgroundDrawable(new BitmapDrawable());
        initView();
    }

    public void initView() {
        title = context.getResources().getStringArray(R.array.fragment_button_pop);
        gridView = (GridView) window.findViewById(R.id.gridView);
        adapter = new FragmentAdapter_POP(context);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                StartIntent.get(context).contentStart(title[position]);
            }
        });
        adapter.setDates(title, imageUrls);
        window.findViewById(R.id.image_off).setOnClickListener(new PerfectClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                dismiss();
            }
        });
        button = (RadioButton) window.findViewById(R.id.button);
        group = (RadioGroup) window.findViewById(R.id.radiogrouproup);
    }

    public void showPopupWindow(View parent, int i) {
        if (!this.isShowing()) {
            WindowManager.LayoutParams params = context.getWindow().getAttributes();
            params.alpha = 0.6f;
            context.getWindow().setAttributes(params);
            button.setChecked(true);
            Log.i("Unions", "执行        ？");
            // 以下拉方式显示popupwindow
            this.showAtLocation(parent, Gravity.BOTTOM, 0, i);
        }
    }

}
